package com.test.thread.executorservice;

import java.util.concurrent.*;

/**
 * Created by jl on 2018/10/17 0017
 */
public class Demo {

    public static final class MyRunnable implements Runnable {

        @Override
        public void run() {
            System.out.println("hello every body");
        }
    }

    public static final class MyRunnable2 implements Runnable {
        @Override
        public void run() {
            System.out.println("hello every body again");
        }
    }

    static final class MyCallable implements Callable<String> {

        @Override
        public String call() throws Exception {
            System.out.println("hello this is callable");
            return "ok";
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        MyRunnable myRunnable = new MyRunnable();
        MyRunnable2 myRunnable2 = new MyRunnable2();
        MyCallable myCallable = new MyCallable();
        executorService.execute(myRunnable);
        executorService.execute(myRunnable2);
        Future<String> a = executorService.submit(myCallable);
        System.out.println(a.get());

        for (int i = 0; i < 120; i++) {
            if (i % 3 == 0) {
                executorService.execute(myRunnable);
            }
            if (i % 3 == 1) {
                executorService.execute(myRunnable2);
            }
            if (i % 3 == 2) {
                executorService.submit(myCallable);
            }
        }

        executorService.shutdown();
    }
}
